<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Connection Quality Monitor</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_example_c_kvrnetworkconnectiontest.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Connection Quality Monitor </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>samples\example\c\kvrNetworkConnectionTest.c</b> </p><div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">** This software is furnished as Redistributable under the Kvaser Software Licence</span></div><div class="line"><span class="comment">** https://www.kvaser.com/canlib-webhelp/page_license_and_copyright.html</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">** Description:</span></div><div class="line"><span class="comment">** This example shows how to get RSSI and RTT values for a connection.</span></div><div class="line"><span class="comment">**/</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="canlib_8h.html">canlib.h</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="kvrlib_8h.html">kvrlib.h</a>&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// List all connected devices</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="keywordtype">void</span> listDevices (<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  <span class="keywordtype">int</span> i;</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> status = <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>;</div><div class="line">  <span class="keywordtype">char</span> name[100];</div><div class="line">  <span class="keywordtype">int</span> channel_count = 0;</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line">  <a class="code" href="group__can__general.html#ga65169ca633cd30aa92b8a80e28a5378b">canGetNumberOfChannels</a>(&amp;channel_count);</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;First argument must be a channel!\n\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;Channel\t Name\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;--------------------------------------------------------\n&quot;</span>);</div><div class="line">  <span class="keywordflow">for</span> (i = 0; (status == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) &amp;&amp; (i &lt; channel_count); i++) {</div><div class="line">    name[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">    status = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(i, <a class="code" href="canlib_8h.html#aa4fc3066c84e718b90e870103837259b">canCHANNELDATA_CHANNEL_NAME</a>, name, <span class="keyword">sizeof</span>(name));</div><div class="line">    printf(<span class="stringliteral">&quot;%d\t %s\n&quot;</span>, i, name);</div><div class="line">  } </div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Get RSSI and RTT values</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="keywordtype">void</span> getRssiRtt (kvrConfigHandle handle, <span class="keywordtype">int</span> *rssi_mean, <span class="keywordtype">int</span> *rtt_mean)</div><div class="line">{</div><div class="line">  kvrStatus status;</div><div class="line">  kvrRssiHistory rssi[14] = {0};</div><div class="line">  kvrRttHistory rtt[14] = {0};  </div><div class="line">  <span class="keywordtype">int</span> rtt_len = <span class="keyword">sizeof</span>(rtt) / <span class="keyword">sizeof</span>(kvrRttHistory);</div><div class="line">  <span class="keywordtype">int</span> rssi_len = <span class="keyword">sizeof</span>(rssi) / <span class="keyword">sizeof</span>(kvrRssiHistory);</div><div class="line">  <span class="keywordtype">int</span> rtt_actual;</div><div class="line">  <span class="keywordtype">int</span> rssi_actual;</div><div class="line"></div><div class="line">  *rssi_mean = 0;</div><div class="line">  *rtt_mean = 0;</div><div class="line">  </div><div class="line">  status = kvrNetworkGetRssiRtt(handle, rssi, rssi_len, &amp;rssi_actual,</div><div class="line">                                rtt, rtt_len, &amp;rtt_actual);</div><div class="line">  <span class="keywordflow">if</span> (status == kvrOK) {</div><div class="line">    <span class="keywordtype">int</span> i;</div><div class="line">    printf(<span class="stringliteral">&quot;RSSI (%d):&quot;</span>, rssi_actual);</div><div class="line">    <span class="keywordflow">for</span>(i = 0; i &lt; rssi_actual; i++) {</div><div class="line">      *rssi_mean += rssi[i];</div><div class="line">      printf(<span class="stringliteral">&quot; %d&quot;</span>, rssi[i]);</div><div class="line">    }</div><div class="line">    printf(<span class="stringliteral">&quot;\nRTT (%d):&quot;</span>, rtt_actual);</div><div class="line">    <span class="keywordflow">for</span>(i = 0; i &lt; rtt_actual; i++) {</div><div class="line">      *rtt_mean += rtt[i];</div><div class="line">      printf(<span class="stringliteral">&quot; %d&quot;</span>, rtt[i]);</div><div class="line">    }</div><div class="line">    </div><div class="line">    printf(<span class="stringliteral">&quot;\n&quot;</span>);</div><div class="line">    </div><div class="line">    <span class="keywordflow">if</span> (rssi_actual) {</div><div class="line">      *rssi_mean = *rssi_mean / rssi_actual;</div><div class="line">    }</div><div class="line">    </div><div class="line">    <span class="keywordflow">if</span> (rtt_actual) {</div><div class="line">      *rtt_mean = *rtt_mean / rtt_actual;</div><div class="line">    }</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// kvrNetworkConnectionTest.exe &lt;channel&gt;</span></div><div class="line"><span class="comment">//------------------------------------------------------------------------------</span></div><div class="line"><span class="keywordtype">int</span> main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">  kvrConfigHandle config_handle;</div><div class="line">  kvrStatus status;</div><div class="line">  <span class="keywordtype">int</span> canlib_channel = 0;</div><div class="line">  <span class="keywordtype">int</span> j;</div><div class="line">  <span class="keywordtype">int</span> rssi_mean, rtt_mean;</div><div class="line">  kvrInitializeLibrary();</div><div class="line"></div><div class="line">  <span class="keywordflow">switch</span> (argc) {</div><div class="line">  <span class="keywordflow">case</span> 1:</div><div class="line">    listDevices();</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">  <span class="keywordflow">case</span> 2:</div><div class="line">    canlib_channel = argv[1][0] - <span class="charliteral">&#39;0&#39;</span>;</div><div class="line">    <span class="keywordflow">break</span>;</div><div class="line">  <span class="keywordflow">default</span>:</div><div class="line">    listDevices(); </div><div class="line">    <span class="keywordflow">return</span> -1;</div><div class="line">  }</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;canlib channel = %d\n&quot;</span>, canlib_channel);</div><div class="line">  </div><div class="line">  status = kvrConfigOpen(canlib_channel, kvrConfigMode_R, <span class="stringliteral">&quot;&quot;</span>, &amp;config_handle);</div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not start config (%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status; </div><div class="line">  }</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;config_handle = %d\n\n&quot;</span>, config_handle);</div><div class="line">  </div><div class="line">  printf(<span class="stringliteral">&quot;kvrNetworkConnectionTest( on )\n\n&quot;</span>);</div><div class="line">  status = kvrNetworkConnectionTest(config_handle, 1); <span class="comment">// Start sending pings.</span></div><div class="line">  <span class="keywordflow">if</span> (status != kvrOK) {</div><div class="line">    printf(<span class="stringliteral">&quot;Could not start connection test (%d)\n&quot;</span>, status);</div><div class="line">    <span class="keywordflow">return</span> status; </div><div class="line">  }</div><div class="line"></div><div class="line">  Sleep(2000); <span class="comment">// Wait for the device to connect.</span></div><div class="line"></div><div class="line">  <span class="keywordflow">for</span> (j = 0; j &lt; 10; j++) {</div><div class="line">    Sleep(2000); <span class="comment">// Wait for some pings to be sent.</span></div><div class="line">    getRssiRtt(config_handle, &amp;rssi_mean, &amp;rtt_mean);</div><div class="line">    printf(<span class="stringliteral">&quot;rssi_mean = %d\n&quot;</span>, rssi_mean);    </div><div class="line">    printf(<span class="stringliteral">&quot;rtt_mean = %d\n\n&quot;</span>, rtt_mean);   </div><div class="line">  }</div><div class="line">  </div><div class="line">  status = kvrNetworkConnectionTest(config_handle, 0); <span class="comment">// Stop sending pings.</span></div><div class="line">  printf(<span class="stringliteral">&quot;Done!\n&quot;</span>);</div><div class="line">  kvrConfigClose(config_handle);</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_kvrlib.html">Remote Device API (kvrlib)</a></li><li class="navelem"><a class="el" href="page_user_guide_kvrlib_samples.html">Sample Programs (kvrlib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
